package com.tz.crm.dao.impl;

import com.tz.crm.dao.ContractDao;
import com.tz.crm.entity.Contract;
import com.tz.crm.entity.Payment;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 使用 @Repository 将 DAO 类声明为 Bean
 */
@Repository
@Transactional
public class ContractDaoImpl extends BaseDaoImpl<Contract, String> implements ContractDao {

    @Override
    public Contract getByNum(String contractNum) {
        String hql = "from Contract c where c.inNum = :inNum";
        return (Contract) getSession().createQuery(hql)
                .setParameter("inNum", contractNum)
                .uniqueResult();
    }
    @Override
    public int getCount(Contract contract) {
        String hql="select count(*) from Contract where 1=1";
        List<Object> objs=new ArrayList<>();
        if(contract.getContractTemplate().getName() != null){
            hql+=" and contractTemplate.name=?";
            objs.add(contract.getContractTemplate().getName());
        }
        if(contract.getSignUser().getRealName() != null){
            hql+=" and signUser.realName=?";
            objs.add(contract.getSignUser().getRealName());
        }

        Query query = getSession().createQuery(hql);
        //如果集合存在,就发送该参数
        if(objs!=null && objs.size()>0){
            for (int i = 0; i < objs.size(); i++) {
                query.setParameter(i, objs.get(i));
            }
        }
        Long count = (Long) query.uniqueResult();
        int total = count.intValue();
        return total;
    }

    @Override
    /**
     * 根据月份计算金额的最大量
     */
    public double getSumMoney(int awayMonth) {
        String hql = "select sum(c.amount) from Contract c where createDate between :myFromDate and :myToDate";
        Calendar firstDay = Calendar.getInstance();
        Calendar lastDay = Calendar.getInstance();
        firstDay.set(firstDay.get(Calendar.YEAR), firstDay.get(Calendar.MONTH) - awayMonth, 1);
        lastDay.set(lastDay.get(Calendar.YEAR), lastDay.get(Calendar.MONTH) - awayMonth + 1, 0);
        Query query = getSession().createQuery(hql);
        query.setParameter("myFromDate", firstDay.getTime());
        query.setParameter("myToDate", lastDay.getTime());
        if (query.uniqueResult() == null) {
            return 0.0;
        }
        return Double.valueOf(query.uniqueResult() + "");
    }

    @Override
    /**
     * 根据支付名称,顾客姓名 查找付款计划的集合
     */
    public List<Payment> select(String payName, String inNum, String customerName, String signUser) {
        String hql="select p from Payment p join p.contract c join c.signUser s where 1=1";
        List<Object> objs=new ArrayList<>();
        if(StringUtils.isNotBlank(inNum.trim())){
            hql+=" and c.inNum=?";
            objs.add(inNum);
        }
        if(StringUtils.isNotBlank(customerName.trim())){
            hql+=" and c.customerName=?";
            objs.add(customerName);
        }
        if(StringUtils.isNotBlank(signUser.trim())){
            hql+=" and s.id=?";
            objs.add(signUser);
        }
        Query<Payment> query = getSession().createQuery(hql, Payment.class);
        //如果集合存在,就发送该参数
        if(objs!=null && objs.size()>0){
            for (int i = 0; i < objs.size(); i++) {
                query.setParameter(i, objs.get(i));
            }
        }
        return query.list();
    }
}
